using UnityEngine;
using System.Collections;

public class CountDown : MonoBehaviour {

    public int defaultTime;
    public TextMesh textMesh;
    public delegate void ExecuteWhenDoneDelegate();

    public ExecuteWhenDoneDelegate ExecuteWhenDone;

	// Use this for initialization
	void Start () {
	    
	}
	
	// Update is called once per frame
	void Update () {

        this.transform.LookAt(Camera.main.transform);

	}

    public void StartCounting(float timeLeft = 5)
    {
        StartCoroutine(Counting(timeLeft));
    }

    private IEnumerator Counting(float timeLeft)
    {
        while (timeLeft > 0.1)
        {
            this.textMesh.text = timeLeft.ToString("n1");
            yield return new WaitForSeconds(0.1f);
            timeLeft -= 0.1f;
        }

        this.ExecuteWhenDone();
    }
}
